﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.EmailsAndWorkflows
{
    public partial class WorkflowDocumentTypes : BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        decimal APPLICATION_WORFLOW_ID
        { get; set; }
        public WorkflowDocumentTypes()
        {
            InitializeComponent();
            lstDocumentTypes.Items.Clear();
            try
            {
                PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DOCUMENT_TYPESLoadAll().ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_ARCHIVE_DOCUMENT_TYPESLoadAllResult>(delegate(PRO_DOCS.DAL.Database.proc_ARCHIVE_DOCUMENT_TYPESLoadAllResult _dOCUMENT_TYPE)
                {
                    CheckedListBoxItem listViewItem = new CheckedListBoxItem();
                    listViewItem.Value = _dOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID.ToString();
                    listViewItem.Description = _dOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NAME;
                    lstDocumentTypes.Items.Add(listViewItem);
                }));
            }
            catch { }
        }

        public void LOAD_WORKFLOW_DOCUMENT_TYPES(decimal aPPLICATION_WORFLOW_ID)
        {
            APPLICATION_WORFLOW_ID = aPPLICATION_WORFLOW_ID;
            lstDocumentTypes.UnCheckAll();

            Dictionary<string, DAL.Database. proc_APPLICATION_WORKFLOW_ARCHIVE_DOCUMENT_TYPESLoadByAPPLICATION_WORFLOW_IDResult> aPPLICATION_WORKFLOW_DOCUMENT_TYPES = PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOW_ARCHIVE_DOCUMENT_TYPESLoadByAPPLICATION_WORFLOW_ID(aPPLICATION_WORFLOW_ID).ToDictionary(k => k.ARCHIVE_DOCUMENT_TYPE_ID.ToString(), v => v);
            for (int i = 0; i < lstDocumentTypes.Items.Count; i++)
            {
                bool checked_status = aPPLICATION_WORKFLOW_DOCUMENT_TYPES.ContainsKey(lstDocumentTypes.Items[i].Value.ToString());
                lstDocumentTypes.SetItemChecked(i, checked_status);
            }

        }

        public void SAVE_WORKFLOW_DOCUMENT_TYPES(decimal aPPLICATION_WORFLOW_ID)
        {
            APPLICATION_WORFLOW_ID = aPPLICATION_WORFLOW_ID;
            //1. delete previous saved
            PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOW_ARCHIVE_DOCUMENT_TYPESDeleteByAPPLICATION_WORFLOW_ID(APPLICATION_WORFLOW_ID);
            //2. Save new data
            for (int i = 0; i < lstDocumentTypes.Items.Count; i++)
            {
                if (lstDocumentTypes.GetItemChecked(i))
                {
                    PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOW_ARCHIVE_DOCUMENT_TYPESInsert
                        (
                            APPLICATION_WORFLOW_ID,
                            Convert.ToInt64(lstDocumentTypes.GetDisplayItemValue(i).ToString())
                        );
                }
            }
        }

        internal void CLEAR_SELECTED_DOCUMENT_TYPES()
        {
            lstDocumentTypes.UnCheckAll();
        }
    }
}
